package com.sparkseries.controller;

import com.sparkseries.common.util.domain.Result;
import com.sparkseries.domain.vo.SysNoticeVO;
import com.sparkseries.dto.SysNoticeDTO;
import com.sparkseries.service.SysNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("notice")
@Tag(name = "通知管理")
public class SysNoticeController {
    @Resource
    private SysNoticeService sysNoticeService;

    /**
     * 推送通知
     * 推送保存的草稿时需要传输通知的id
     *
     * @param sysNoticeDTO 通知传输类
     * @return 默认响应类
     */
    @PostMapping("createNotice")
    @Operation(summary = "推送通知")
    //@PreAuthorize("")
    public Result<?> createNotice(@RequestBody SysNoticeDTO sysNoticeDTO) {
        return sysNoticeService.createNotice(sysNoticeDTO);
    }

    /**
     * 撤回推送通知
     *
     * @param noticeId 通知id
     * @return 默认响应类
     */
    @DeleteMapping("revokeNotice/{noticeId}")
    @Operation(summary = "撤回推送通知")
    //@PreAuthorize("")
    public Result<?> revokeNotice(@PathVariable("noticeId") Long noticeId) {
        return sysNoticeService.revokeNotice(noticeId);
    }

    /**
     * 删除推送通知
     *
     * @param noticeId 通知id
     * @return 默认响应类
     */
    @DeleteMapping("deleteNotice/{noticeId}")
    @Operation(summary = "删除推送通知")
    //@PreAuthorize("")
    public Result<?> deleteNotice(@PathVariable("noticeId") Long noticeId){
        return sysNoticeService.deleteNotice(noticeId);
    }

    /**
     * 删除我的通知
     *
     * @param noticeId 通知ID
     * @return 默认响应类
     */
    @DeleteMapping("deleteMyNotice/{noticeId}")
    @Operation(summary = "删除我的通知")
    //@PreAuthorize("")
    public Result<?> deleteMyNotice(@PathVariable("noticeId") Long noticeId){
        return sysNoticeService.deleteMyNotice(noticeId);
    }

    /**
     * 修改推送通知
     * 修改存为草稿或撤回的通知
     *
     * @param sysNoticeDTO 通知传输类
     * @return 默认响应类
     */
    @PutMapping("updateNotice")
    @Operation(summary = "修改推送通知")
    //@PreAuthorize("")
    public Result<?> updateNotice(@RequestBody SysNoticeDTO sysNoticeDTO) {
        return sysNoticeService.updateNotice(sysNoticeDTO);
    }

    /**
     * 修改推送通知状态
     * 0删除 1删除 2草稿 3撤回
     *
     * @param noticeId 通知id
     * @param status 状态
     * @return 默认响应类
     */
    @PutMapping("updateNoticeStatus/{noticeId}")
    @Operation(summary = "修改推送通知状态")
    //@PreAuthorize("")
    public Result<?> updateNoticeStatus(@PathVariable("noticeId") Long noticeId,@RequestParam("status") Integer status){
        return sysNoticeService.updateNoticeStatus(noticeId,status);
    }

    /**
     * 我的消息标记为已读
     *
     * @param noticeId 通知id
     * @return 默认响应类
     */
    @PutMapping("updateReadStatus/{noticeId}")
    @Operation(summary = "我的消息标记为已读")
    //@PreAuthorize("")
    public Result<?> updateReadStatus(@PathVariable("noticeId") Long noticeId) {
        return sysNoticeService.updateReadStatus(noticeId);
    }

    /**
     * 获取推送通知
     *
     * @param noticeId 通知id
     * @return 通知响应类
     */
    @GetMapping("getNoticeById/{noticeId}")
    @Operation(summary = "获取推送通知")
    //@PreAuthorize("")
    public Result<SysNoticeVO> getNoticeById(@PathVariable("noticeId") Long noticeId){
        return sysNoticeService.getNoticeById(noticeId);
    }

    /**
     * 获取我的通知
     *
     * @param noticeId 通知id
     * @return 通知响应类
     */
    @GetMapping("getMyNoticeById/{noticeId}")
    @Operation(summary = "获取我的通知")
    //@PreAuthorize("")
    public Result<SysNoticeVO> getMyNoticeById(@PathVariable("noticeId") Long noticeId){
        return sysNoticeService.getMyNoticeById(noticeId);
    }

    /**
     * 推送通知列表
     *
     * @param sysNoticeDTO 通知传输类
     * @return 通知响应类集合
     */
    @GetMapping("listNotice")
    @Operation(summary = "推送通知列表")
    //@PreAuthorize("")
    public Result<List<SysNoticeVO>> listNotice(@RequestBody(required = false) SysNoticeDTO sysNoticeDTO) {
        return sysNoticeService.listNotice(sysNoticeDTO);
    }

    /**
     * 我的通知列表
     *
     * @param sysNoticeDTO 通知传输类
     * @return 通知响应类集合
     */
    @GetMapping("listMyNotice")
    @Operation(summary = "我的通知列表")
    //@PreAuthorize("")
    public Result<List<SysNoticeVO>> listMyNotice(@RequestBody(required = false) SysNoticeDTO sysNoticeDTO) {
        return sysNoticeService.listMyNotice(sysNoticeDTO);
    }

    /**
     * 某通知的已读用户和未读用户列表
     *
     * @param noticeId 通知id
     * @return map集合，键为用户名，值为读取状态
     */
    @GetMapping("listNoticeReadUsersAndUnReadUsers/{noticeId}")
    @Operation(summary = "某通知的已读用户和未读用户列表")
    //@PreAuthorize("")
    public Result<Map<String,Integer>> listNoticeReadUsersAndUnReadUsers(@PathVariable("noticeId")Long noticeId){
        return sysNoticeService.listNoticeReadIdsAndUnReadIds(noticeId);
    }
}
